package com.hzh.homeWork

import scala.io.Source

/**
 * 1、统计班级人数
 *
 */
object DemoWork1 {

  def main(args: Array[String]): Unit = {

    //读文件
    val lines: List[String] = Source.fromFile("data/students.txt").getLines().toList
    //切分出班级
    val clazz: List[String] = lines.map((lines: String) => {
      val split: Array[String] = lines.split(",")
      split(4)
    })
    //按班级分组
    val groupBy: Map[String, List[String]] = clazz.groupBy((word: String) => word)
    //map
    val clazzCount: Map[String, Int] = groupBy.map((kv: (String, List[String])) => {
      val clazz: String = kv._1
      val values: List[String] = kv._2
      //计算班级的数量
      val count: Int = values.length
      //返回班级和班级的数量
      (clazz, count)
    })
    clazzCount.foreach(println)


  }

}
